
#ifndef __RTSP_SER_H
#define __RTSP_SER_H

#include "public_def.h"

#include "base64.h"
#include "sock.h"
#include "arrayctrl.h"
#include "rtp.h"
#include "rtspserver.h"

typedef enum 
{
	AUTHORIZED_NONE,
	AUTHORIZED_BASE64,
	AUTHORIZED_DIGEST
}AUTHORIZED_MODE;

#define RTSP_AUTH_METHOD AUTHORIZED_BASE64

#define RTSP_CMD_INVALID -1
#define RTSP_TCP_VIDEO_RTP 0
#define RTSP_TCP_VIDEO_RTCP 1
#define RTSP_TCP_AUDIO_RTP 2
#define RTSP_TCP_AUDIO_RTCP 3

//Enumeration of RTSP commands
typedef enum EnumRTSP_CMD
{
	RTSP_CMD_SETUP = 0,
	RTSP_CMD_TEARDOWN,
	RTSP_CMD_OPTIONS,
	RTSP_CMD_PLAY,
	RTSP_CMD_PAUSE,
	RTSP_CMD_DESCRIBE,
	RTSP_CMD_RTSP,
	RTSP_CMD_SETPARAMETER,
	RTSP_CMD_LAST
} EnumRTSP_CMD_t;

//Enumeration of RTSP header
typedef enum EnumRTSP_HDR
{
	RTSP_HDR_RTSP = 0, 
	RTSP_HDR_CSEQ,
	RTSP_HDR_PUBLIC, 
	RTSP_HDR_DATE,
	RTSP_HDR_EXPIRES,
	RTSP_HDR_TRANSPORT,
	RTSP_HDR_X_DYNAMIC_RATE,
	RTSP_HDR_LOCATION,
	RTSP_HDR_SUPPORTED,
	RTSP_HDR_AUTH,

	RTSP_HDR_LAST
} EnumRTSP_HDR_t;

//Enumeration of RTSP parameter
typedef enum EnumRTSP_PARAM
{
	RTSP_PARAM_CLIENT_PORT = 0, 
	RTSP_PARAM_TRACKID,
	RTSP_PARAM_INTERLEAVED,
	RTSP_PARAM_RTPOVERTCP,
	RTSP_PARAM_LAST
} EnumRTSP_PARAM_t;

//Enumeration of RTSP return code
typedef enum EnumRTSP_CODE
{
	RTSP_CODE_CONTINUE = 0,
	RTSP_CODE_OK, 
	RTSP_CODE_CREATED, 
	RTSP_CODE_ACCEPTED,
	RTSP_CODE_NONAUTHORITATIVE_INFORMATION,
	RTSP_CODE_NO_CONTENT,
	RTSP_CODE_RESET_CONTENT,
	RTSP_CODE_PARTIAL_CONTENT,
	RTSP_CODE_MULTIPLE_CHOICES,
	RTSP_CODE_MOVED_PERMANENTLY,
	RTSP_CODE_MOVED_TEMPORARILY,
	RTSP_CODE_UNAUTHORIZED,
	RTSP_CODE_PAYMENT_REQUIRED,
	RTSP_CODE_METHOD_NOT_ALLOWED,
	RTSP_CODE_NOT_ACCEPTABLE,
	RTSP_CODE_PROXY_AUTHENTICATION_REQUIRED,
	RTSP_CODE_REQUEST_TIMEOUT,
	RTSP_CODE_CONFLICT,
	RTSP_CODE_GONE,
	RTSP_CODE_LENGTH_REQUIRED,
	RTSP_CODE_PRECONDITION_FAILED,
	RTSP_CODE_REQUEST_ENTITY_TOO_LARGE,
	RTSP_CODE_REQUEST_URI_TOO_LARGE,
	RTSP_CODE_UNSUPPORTED_MEDIA_TYPE,
	RTSP_CODE_BAD_EXTENSION,
	RTSP_CODE_INVALID_PARAMETER,
	RTSP_CODE_PARAMETER_NOT_UNDERSTOOD,
	RTSP_CODE_CONFERENCE_NOT_FOUND,
	RTSP_CODE_NOT_ENOUGH_BANDWIDTH,
	RTSP_CODE_METHOD_NOT_VALID_IN_THIS_STATE,
	RTSP_CODE_HEADER_FIELD_NOT_VALID_FOR_RESOURCE,
	RTSP_CODE_INVALID_RANGE,
	RTSP_CODE_PARAMETER_IS_READONLY,
	RTSP_CODE_UNSUPPORTED_TRANSPORT,
	RTSP_CODE_NOT_IMPLEMENTED,
	RTSP_CODE_BAD_GATEWAY,
	RTSP_CODE_SERVICE_UNAVAILABLE,
	RTSP_CODE_GATEWAY_TIMEOUT,
	RTSP_CODE_RTSP_VERSION_NOT_SUPPORTED,
	RTSP_CODE_EXTENDED_ERROR,
	RTSP_CODE_NOTFOUND,
} EnumRTSP_CODE_t;

//Enumeration of RTSP variable type
typedef enum RTSP_Var_Type 
{
	INTEGER = 0,
	STRING
} RTSP_Var_Type_t;

//RTSP command message
typedef struct Msg_RTSP_CMD 
{
	EnumRTSP_CMD_t id; 
	char * msg;
} Msg_RTSP_CMD_t;

//RTSP command header
typedef struct Msg_RTSP_HDR 
{
	EnumRTSP_HDR_t id; 
	char * msg;
} Msg_RTSP_HDR_t;

//RTSP command parameter
typedef struct Msg_RTSP_PARAM 
{
	EnumRTSP_PARAM_t id; 
	char * msg;
} Msg_RTSP_PARAM_t;

//RTSP return code
typedef struct Msg_RTSP_CODE 
{
	EnumRTSP_CODE_t id; 
	char * msg;
	int code;
} Msg_RTSP_CODE_t;
#define RTSP_USERNAME_LEN 32
#define	RTSP_PASSWOED_LEN 32
#define RTSP_REALM_LEN 32

//RTSP connection defination
typedef struct RTSPConnection
{
	EnumRTSP_CMD_t commandID, lastCommandID;
	uint32 CSeq;
	uint32 sessionID;
	uint32 versionID;
	uint32 sdpSessionID;
	uint32 rtpAPort;
	uint32 rtpVPort;
	uint32 trackID;
	uint32 sdpVersionID;
	sint32 chID;
	char addr[64];
	char contentBase[255];
	char remoteip[16];
	char localIP[16];
	char supported[255];
	bool ispostflag;
	ClientSock_t artpSock;
	ClientSock_t vrtpSock;
	ClientSock_t artcpSock;
	ClientSock_t vrtcpSock;
	ClientSock_t rtspSock;
	
	bool active;
	uint8 x_dynamic_rate;
	struct HI_CHInfo * devCh;
	uint16 commandCode;
	bool dynamicRateCalled;
	bool waitingFor501;
	uint8 cntWaitingFor501;
	bool waitingForIDR;
	bool waitedIDR;
	bool isOverTcp;
	bool isHaveVideo;
	bool WatchDogOn;
	time_t lastAccess;
	time_t lastPlay;
	int m_iSendErrorCnt;
	pthread_t m_tPthreadSend;
	RTPSession_t rtpSession;
	
	#if (RTSP_AUTH_METHOD== AUTHORIZED_BASE64 ||RTSP_AUTH_METHOD== AUTHORIZED_DIGEST)
	char username[RTSP_USERNAME_LEN + 1];
	char password[RTSP_PASSWOED_LEN + 1];
	char realm[RTSP_REALM_LEN + 1];
	#elif RTSP_AUTH_METHOD == AUTHORIZED_DIGEST
	char anonce[RTSP_REALM_LEN + 1];
	#endif
} RTSPConnection_t;

//Callback function for acception of client connection
//
//Parameter:
//	pSock		Remoct client socket
//	pParam		Reference of input parameter
//
//Return:
//	None.
//
 int RTSPSvrSocketAcceptCallBack(ClientSock_t * pSock, void * pParam);

//Callback function for receiving data from remote client
//
//Parameter:
//	pBuf		data start address
//	len 		data length
//	pSock		Remote socket
//	iPort		Local port
//	pParam		Reference of input parameter
//
//Return:
//	0 succ.
//
 int RTSPSvrSocketRecvCallBack(void * pBuf, int len, ClientSock_t *pSock, int iPort, void *pParam);

//Callback function for removing object in a dynamic array.
//
//Parameter:
//	pParam		Object reference
//	
//Return:
//	None.
//
 void OnAC_Destroy_RTSPConnection(void * pParam);

//Callback function for disconnection of client connection
//
//Parameter:
//	pSock		Remoct client socket
//	pParam		Reference of input parameter
//
//Return:
//	None.
//
 void RTSPClientSocketDisconnectCallBack(ClientSock_t *pSock, void *pParam);

//Mark the last access timestamp in a RTSP connection
//
//Parameter:
//	pConn		RTSP connection
//	
//Return:
//	None.
//
 void RTSPMarkLastAccess(RTSPConnection_t * pConn);

 void InitRTSPConnection(RTSPConnection_t * pConn);
 int RTSPParser(RTSPConnection_t * pConn, char * pBuf, int len);
 void RTSPCloseConnectionSocks(RTSPConnection_t * pConn);
 int SendClientData(char *_pInputBuffer, int _iLength, RTSPConnection_t *_pconn, const char _chChannel);
RTSPConnection_t * GetRTSPConnection(RTSPServer_t * pServer, ClientSock_t * pSock);

#endif
